/*******************************************************************************
 * 
 * Copyright:
 * 
 * License:
 * 
 * Authors: SALMON Charles
 * 
 ******************************************************************************/

/*******************************************************************************
 * 
 ******************************************************************************/

/**
 * A form to handle the answers of a given survey
 */
qx.Class.define("bip.ui.survey.DeprecatedFillSurveyForm",
{
  extend : qx.ui.form.Form,
  
  construct : function()
  {
    this.base(arguments);
  },
  
  /*
	 * ****************************************************************************
	 * PROPERTIES
	 * ****************************************************************************
	 */
	properties : {
		state : {
			check : [ "completed", "new", "on-progress", "error", "init" ],
			init : "init",
			event : "changeState"
		}
	},
  
	/*
	 *****************************************************************************
    MEMBERS
	 *****************************************************************************
	 */

 	members :
 	{
 		/**
 		 * Build the form
 		 * Altre the state of the form
 		 */
 		build: function(survey) {
 			var q = 1;
 			var currentTopicId;
 			var nQuestions = 0;
 			var nAnswers = 0;
 			for(var i=0;i<survey.length;i++) {
 				var model = survey.getItem(i);
 				if (model.getISurveyMetaData()) {
 					this.addGroupHeader("Survey");
 					var date = new qx.util.format.DateFormat(qx.locale.Date.getDateFormat("long", qx.locale.Manager.getInstance().getLocale()));
 					var dateField = new qx.ui.form.TextField(date.format(new Date(model.getUserDate().getTime())));
 					dateField.setReadOnly(true);
 					this.add(dateField,"Date",null,"userDate");
 					nQuestions = model.getNumberOfQuestions();
 					nAnswers = model.getNumberOfAnswers();
 				}
 				else if (model.getITopic()) {
 					currentTopicId = model.getIdTopic(); 
 					this.addGroupHeader(model.getName());
 				} else if (model.getIQuestion()) {
 					//this.debug(qx.dev.Debug.debugProperties(model));
 					var question = new bip.ui.form.survey.QuestionField(model.getMinValue(),model.getMaxValue());
 					question.set({
 							name: model.getName(),
 				    	description:model.getDescription(),
 				    	value: model.getUserValue()
 				    });
 				    question.setWidth(200);
 				    this.add(question,"Question "+q,null,"q_"+currentTopicId+"_"+model.getIdQuestion());
 				    q++;
 				}
 			}
 			if (nAnswers == 0) {this.setState("new");}
 			else if (nAnswers < nQuestions) {this.setState("on-progress");}
 			else if (nAnswers == nQuestions) {this.setState("completed");}
 			else {this.setState("error");}
 			
 		}
 	},
 	
	/*
	 * ****************************************************************************
	 * DESTRUCTOR
	 * ****************************************************************************
	 */

	destruct : function() {
		this._disposeObjects()
	}

  
});